package band.wukong.practice.javase.core.io.readerandwriter;

import java.io.ByteArrayInputStream;
import java.nio.charset.Charset;

/**
 * Desc:
 * Author: sunjs
 * Date: 2009-11-16 15:14:38
 */
public class TestEncode {
    public static void main(String[] args) throws Exception {
        System.out.println(System.getProperty("file.encoding"));
        System.out.println(Charset.defaultCharset());

        System.out.println("在内存中采用Unicode字符编码：");
        char c = '道';
        int lowBit = c & 0xFF00;    //获得二进制的低8位
        int highBit = (c & 0xFF00) >> 8;    //获得二进制的高8位
        System.out.println("highBit = " + highBit);
        System.out.println("lowBit = " + lowBit);

        String s = "道";
        System.out.println("采用本地操作系统默认字符编码：");
        readBuff(s.getBytes());
        readBuff(s.getBytes());

        System.out.println("采用GBK字符编码：");
        readBuff(s.getBytes("GBK"));

        System.out.println("采用UTF-8字符编码：");
        readBuff(s.getBytes("UTF-8"));

    }

    private static void readBuff(byte[] buff) throws Exception {
        ByteArrayInputStream in = new ByteArrayInputStream(buff);
        int data;
        while ((data = in.read()) != -1) {
            System.out.println(data + " ");
        }
        System.out.println();
        in.close();
    }
}
